/**
 * SpringMVC restful 风格
 * 订单管理控制器，控制关于订单的一些业务逻辑操作
 * @author yanwl
 * @date 2014-10-08
 */
package com.chnbst.bstyun.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.CreateOrderNum;
import com.chnbst.bstyun.core.common.DateUtil;
import com.chnbst.bstyun.core.common.PriceUtil;
import com.chnbst.bstyun.core.common.ReadProperties;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.CreateNum;
import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.model.pojo.Cashflow;
import com.chnbst.bstyun.model.pojo.Deal;
import com.chnbst.bstyun.model.pojo.Email;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.GoodsRelation;
import com.chnbst.bstyun.model.pojo.Order;
import com.chnbst.bstyun.model.pojo.Supplier;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.GoodsVo;
import com.chnbst.bstyun.model.vo.OrderVo;
import com.chnbst.bstyun.service.CashflowService;
import com.chnbst.bstyun.service.DealService;
import com.chnbst.bstyun.service.EmailService;
import com.chnbst.bstyun.service.GoodsRelationService;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.SupplierService;
import com.chnbst.bstyun.service.UserService;
import com.chnbst.bstyun.service.VoucherService;

@Controller
@RequestMapping("/myservice")
public class MyServiceController {
	/**
	 * 每页显示的订单条数
	 */
	//private int orderPageSize = 5;
	
	/**
	 * 注入业务逻辑层
	 */
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private VoucherService voucherService;
	@Autowired
	private DealService dealService;
	@Autowired
	private CashflowService cashflowService;
	@Autowired
	private CreateNum createNum;
	@Autowired
	private Eos eos;
	@Autowired
	private ReadProperties readProperties;
	@Autowired
	private GoodsRelationService goodsRelationService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private EmailService emailService;
	
	@RequestMapping(value="/order/upgradeManage",method=RequestMethod.GET)
	public ModelAndView upgradeManage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("users/myservice/upgrade_manage");
		return mav;
	}
	
	@RequestMapping(value="/order/renewManage",method=RequestMethod.GET)
	public ModelAndView renewManage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("users/myservice/renew_manage");
		return mav;
	}
	
	/**
	 * ajax请求 查询用户需要升级的订单信息
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/upgradeManage/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upgradeList(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		map.put("userId", user.getUserId());
		map.put("overTime", null);
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("groupField", "t.user_id,t.goods_id");
		List<OrderVo> orders = orderService.selectLastOrdersByUserId(map);
		map.put("orders", orders);
		return map;
	}
	
	/**
	 * ajax请求 查询用户需要续费的订单信息
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/renewManage/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> renewList(String day,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		map.put("userId", user.getUserId());
		map.put("overTime", SignUtil.getOvertime(5, +Integer.valueOf(day), new Date()));
		map.put("doType", 1);
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("groupField", "t.user_id,t.goods_id");
		List<OrderVo> orders = orderService.selectLastOrdersByUserId(map);
		map.put("orders", orders);
		return map;
	}
	
	/**
	 * ajax请求 查询用户需要在延长续费时间内续费的订单信息
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/extendRenewManage/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> extendRenewList(String overTime,HttpServletRequest request){
		//延长续费通知邮件
		Email email = emailService.selectEmail(42);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		map.put("userId", user.getUserId());
		map.put("day", email.getTimeSpace());
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("groupField", "t.user_id,t.goods_id");
		List<OrderVo> orders = orderService.selectExtendRenewOrderVo(map);
		map.put("orders", orders);
		return map;
	}
	
	/**
	 * ajax请求 检测订单是否可以升级
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/checkOrderUpgrade",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkOrderUpgrade(String orderId,HttpServletRequest request){
		boolean isSuccess = true;
		Order entity = orderService.selectOrderByOrderId(Integer.valueOf(orderId));
		Goods goods = goodsService.selectByGoodsId(entity.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(entity.getOverTime().getTime() > new Date().getTime()){
			if(!"0".equals(goodsRalateGoods)) {
				//根据商品Id和用户Id查找最近一条订单记录
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("goodsId", goodsRalateGoods);
				map.put("userId", entity.getUserId());
				map.put("orderState", 1);
				map.put("sortField", "pay_time");
				map.put("sortOrder", "desc");
				
				Order order = orderService.selectOrderByGoodsId(map);
				
				if(entity.getUserNum() >= order.getUserNum()) {
					isSuccess = false;
					resultMap.put("message", "当前用户数已达到上线，如需继续升级，请先升级该商品关联的商品！！");
				}
			}else {
				if(entity.getUserNum() >= goods.getUsersMax()) {
					isSuccess = false;
					resultMap.put("message", "当前购买的用户已达到商品用户的上线，不能进行升级了！！");
				}
			}
		}else{
			isSuccess = false;
			resultMap.put("message", "当前产品已经到期，请先续费再进行升级！！");
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	/**
	 * ajax请求 检测订单是否可以续费
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/checkOrderRenew",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkOrderRenew(String orderId,HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean isSuccess = true;
		Order order = orderService.selectOrderByOrderId(Integer.valueOf(orderId));
		Goods goods = goodsService.selectByGoodsId(order.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		if(!"0".equals(goodsRalateGoods)) {
			//根据商品Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goodsId", goodsRalateGoods);
			map.put("userId", order.getUserId());
			map.put("orderState", 1);
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			
			Order entity = orderService.selectOrderByGoodsId(map);
			
			long overTime = order.getOverTime().getTime();
			long maxOverTime = entity.getOverTime().getTime();
			
			if(overTime >= maxOverTime) {
				isSuccess = false;
				resultMap.put("str", "当前使用时间已达到上线，如需继续续费，请先续费该产品加入的套餐！");
			}
		}else {
			if("永久".equals(order.getUseLife())) {
				isSuccess = false;
				resultMap.put("str", "永久性套餐不能续费！");
			}
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	
	/**
	 * http://xxx.com/myservice/order/toUpOrderPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 */
	@RequestMapping(value="/order/toUpgradePage",method=RequestMethod.GET)
	public ModelAndView toUpgradePage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		OrderVo orderVo = orderService.selectOrderVoByOrderId(orderId);
		
		List<GoodsRelation> list = goodsRelationService.selectByGoodsId(orderVo.getGoodsId());
		String products = "";
		for (int i = 0; i < list.size(); i++) {
			products += list.get(i).getProductName()+"*1";
			if(i < list.size()-1) {
				products += "+";
			}
		}
		orderVo.setProIds(products);//商品包含的产品组合
		
		GoodsVo goods = goodsService.selectGoodsVoByGoodsId(orderVo.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		int flag = 0; //商品没有关联商品
		if(!"0".equals(goodsRalateGoods)) {
			flag = 1; //商品有关联商品
			//根据商品Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goodsId", goodsRalateGoods);
			map.put("userId", orderVo.getUserId());
			map.put("orderState", 1);
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			Order order = orderService.selectOrderByGoodsId(map);
			mav.addObject("order", order);
		}
		//根据商品Id和用户Id查找试用订单记录
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("goodsId", orderVo.getGoodsId());
		map1.put("userId", orderVo.getUserId());
		Order orderFree = orderService.selectOrderByContion(map1);
		if(orderFree != null) {
			mav.addObject("freeOverTime", orderFree.getOverTime());
		}else {
			mav.addObject("freeOverTime", null);
		}
		
		mav.addObject("orderVo", orderVo);
		mav.addObject("goods", goods);
		mav.addObject("flag", flag);
		mav.setViewName("users/myservice/upgrade_order");
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toRenewPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 */
	@RequestMapping(value="/order/toRenewPage",method=RequestMethod.GET)
	public ModelAndView toRenewPage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		OrderVo orderVo = orderService.selectOrderVoByOrderId(orderId);
		
		List<GoodsRelation> list = goodsRelationService.selectByGoodsId(orderVo.getGoodsId());
		String products = "";
		for (int i = 0; i < list.size(); i++) {
			products += list.get(i).getProductName()+"*1";
			if(i < list.size()-1) {
				products += "+";
			}
		}
		orderVo.setProIds(products);//商品包含的产品组合
		
		GoodsVo goods = goodsService.selectGoodsVoByGoodsId(orderVo.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		int flag = 0; //商品没有关联商品
		if(!"0".equals(goodsRalateGoods)) {
			flag = 1; //商品有关联商品
			//根据商品Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goodsId", goodsRalateGoods);
			map.put("userId", orderVo.getUserId());
			map.put("orderState", 1);
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			Order order = orderService.selectOrderByGoodsId(map);
			mav.addObject("order", order);
		}
		mav.addObject("orderVo", orderVo);
		mav.addObject("goods", goods);
		mav.addObject("flag", flag);
		mav.setViewName("users/myservice/renew_order");
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toUpgradePayPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 * @throws Exception 
	 */
	@RequestMapping(value="/order/toUpgradePayPage",method=RequestMethod.GET)
	public ModelAndView toUpgradePayPage(HttpServletRequest request) throws Exception{
		ModelAndView mav = null;
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		int userNum = Integer.valueOf(request.getParameter("userNum"));
		
		Order order = orderService.selectOrderByOrderId(orderId);
		User user = (User)request.getSession().getAttribute("userInfo");
		Supplier supplier = supplierService.selectSupplierBySupplierId(user.getSupplierId());
		
		GoodsVo goods = goodsService.selectGoodsVoByGoodsId(order.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		
		int userNumSpace = userNum - order.getUserNum();
		int month = 0; //现在截止到到期时间的月份数
		double orderMoney = 0.00; //订单金额
		double discount = 0.00; //代理商优惠金额
		double mealPrivilege = 0.00; //代理商无关联商品的优惠比例
		double proPrivilege = 0.00; //代理商有关联商品的优惠比例
		String EOSresult = "";
		
		if(supplier != null && supplier.getIsValid() == 1) {
			mealPrivilege = 1 - supplier.getMealPrivilege();
			proPrivilege = 1 - supplier.getProPrivilege();
		}
		
		//根据商品Id和用户Id查找试用订单记录
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("goodsId", order.getGoodsId());
		map1.put("userId", order.getUserId());
		Order orderFree = orderService.selectOrderByContion(map1);
		Date startTime = new Date();
		if(orderFree != null) {
			startTime = orderFree.getOverTime();
		}
		
		String orderNum = CreateOrderNum.getOrderNum(user.getUserId());
		Order entity = new Order();
		if(!"0".equals(goodsRalateGoods)) {
			//商品有关联商品
			//根据商品Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goodsId", goodsRalateGoods);
			map.put("userId", order.getUserId());
			map.put("orderState", 1);
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			Order lastOrder = orderService.selectOrderByGoodsId(map);
			
			long reOverTime = lastOrder.getOverTime().getTime();
			long overTime = order.getOverTime().getTime();
			if(overTime >= reOverTime) {
				month = Integer.valueOf(DateUtil.getMonth("getMonth", startTime,lastOrder.getOverTime()));
			}else {
				month = Integer.valueOf(DateUtil.getMonth("getMonth", startTime,order.getOverTime()));
			}
			orderMoney = PriceUtil.calculatePrice(goods.getGoodsPrice(), month, userNumSpace, goods.getFeeFormula(), goods.getFeeBasenum());
			discount = proPrivilege * orderMoney;
			
		}else {
			//商品没有关联商品
			month = Integer.valueOf(DateUtil.getMonth("getMonth", startTime,order.getOverTime()));
			orderMoney = PriceUtil.calculatePrice(goods.getGoodsPrice(), month, userNumSpace, goods.getFeeFormula(), goods.getFeeBasenum());
			discount = mealPrivilege * orderMoney;
		}
		
		entity.setBuyType(2);
		entity.setCount(1);
		entity.setIsDredge(0);
		entity.setIsFree(0);
		entity.setGoodsId(order.getGoodsId());
		entity.setOccurTime(new Date());
		entity.setOrderMoney(orderMoney);
		entity.setDiscount(discount);
		entity.setOrderNum(orderNum);
		entity.setOverTime(order.getOverTime());
		entity.setParentId(order.getParentId());
		entity.setUseLife(order.getUseLife());
		entity.setUserId(order.getUserId());
		entity.setUserNum(userNum);
		entity.setPayByAccount(0.00);
		entity.setPayByOther(0.00);
		
		if(orderMoney == 0) {
			entity.setOrderState(1);
			entity.setPayTime(new Date());
			entity.setPayWayId(2);
			entity.setPayMoney(0.00);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//=============================新增一条交易记录开始======================================
			Deal deal = new Deal();
			deal.setDealDate(new Date());
			deal.setDealName(goods.getGoodsName());
			deal.setDealNum(orderNum);
			deal.setDealState("2");
			deal.setDisburseMoney(order.getOrderMoney());
			deal.setIncomeMoney(0.0);
			deal.setPayWayId(2);
			deal.setUserId(order.getUserId());
			deal.setParentId(user.getUserId());
			deal.setIsCancel(0);
			try {
				dealService.insertDeal(deal);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================新增一条交易记录结束======================================
			
			//=============================收支明细增加开始======================================
			//交易收支明细
			Cashflow cashflow = new Cashflow();
			cashflow.setCashflowNum(createNum.createCashflowNum("2"));
			cashflow.setCreateTime(new Date());
			cashflow.setDealType(2);
			cashflow.setDisburseMoney(0.00);
			cashflow.setIncomeMoney(0.00);
			cashflow.setOrderId(entity.getOrderId());
			cashflow.setPayWayId(2);
			cashflow.setThenAccount(user.getAccountMoney());
			cashflow.setUserId(user.getUserId());
			cashflow.setIsCancel(0);
			try {
				cashflowService.insertCashflow(cashflow);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================收支明细增加结束======================================
			
			//=============================普元数据同步开始======================================
			EOSresult = eos.sendProOrMeal(entity);
			if("ok".equals(EOSresult)) {
				//更改订单的开通状态
				Order updateOrder = orderService.selectOrderByOrderId(entity.getOrderId());
				updateOrder.setIsDredge(1);//0失败  1成功
				orderService.updateOrderByOrderId(updateOrder);
			}
			//=============================普元数据同步开始======================================
			
			mav = new ModelAndView("redirect:/buy/successOrder");
		}else {
			entity.setOrderState(0);
			entity.setPayMoney(orderMoney - discount);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mav = new ModelAndView("redirect:/myservice/uporder/submitOrders");
		}
		
		request.getSession().setAttribute("order", entity);
		request.getSession().setAttribute("userInfo", user);
		request.getSession().setAttribute("proMealName", goods.getGoodsName());
		request.getSession().setAttribute("EOSresult", EOSresult);
		request.getSession().setAttribute("BSTCOMPANYINFO", readProperties);
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toRenewPayPage
	 * get请求 产品续费
	 * @return 产品续费页面
	 * @throws Exception 
	 */
	@RequestMapping(value="/order/toRenewPayPage",method=RequestMethod.GET)
	public ModelAndView toRenewPayPage(HttpServletRequest request) throws Exception{
		ModelAndView mav = null;
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		int year = Integer.valueOf(request.getParameter("year"));
		Order order = orderService.selectOrderByOrderId(orderId);
		
		User user = (User)request.getSession().getAttribute("userInfo");
		Supplier supplier = supplierService.selectSupplierBySupplierId(user.getSupplierId());
		
		GoodsVo goods = goodsService.selectGoodsVoByGoodsId(order.getGoodsId());
		String goodsRalateGoods = goods.getGoodsRalateGoods();
		
		String userLife = order.getUseLife(); //使用年限
		Date overTime = order.getOverTime(); //到期时间
		if(overTime.getTime() < new Date().getTime()) {
			overTime = new Date();
		}
		int month = 0; //现在截止到到期时间的月份数
		double orderMoney = 0.00; //订单金额
		double discount = 0.00; //代理商优惠金额
		double mealPrivilege = 0.00; //代理商无关联商品的优惠比例
		double proPrivilege = 0.00; //代理商有关联商品的优惠比例
		String EOSresult = "";
		
		if(supplier != null && supplier.getIsValid() == 1) {
			mealPrivilege = 1 - supplier.getMealPrivilege();
			proPrivilege = 1 - supplier.getProPrivilege();
		}
		
		String orderNum = CreateOrderNum.getOrderNum(user.getUserId());
		Order entity = new Order();
		if(!"0".equals(goodsRalateGoods)) {
			//商品有关联商品
			//根据商品Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goodsId", goodsRalateGoods);
			map.put("userId", order.getUserId());
			map.put("orderState", 1);
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			Order lastOrder = orderService.selectOrderByGoodsId(map);
			
			Date proNewOverTime = SignUtil.getOvertime(1, +year,overTime);
			if(proNewOverTime.getTime() >= lastOrder.getOverTime().getTime()) {
				month = Integer.valueOf(DateUtil.getMonth("getMonth", overTime,lastOrder.getOverTime()));
				userLife = DateUtil.getMonth("getOverTime", overTime,lastOrder.getOverTime());
				overTime = lastOrder.getOverTime();
			}else {
				month = year*12;
				userLife = DateUtil.getMonth("getOverTime", overTime,proNewOverTime);
				overTime = proNewOverTime;
			}
			
			orderMoney = PriceUtil.calculatePrice(goods.getGoodsPrice(), month, order.getUserNum(), goods.getFeeFormula(), goods.getFeeBasenum());
			discount = proPrivilege * orderMoney;
		}else {
			//商品没有关联商品
			userLife = year+"年";
			overTime = SignUtil.getOvertime(1, +year,overTime);
			month = year*12;
			orderMoney = PriceUtil.calculatePrice(goods.getGoodsPrice(), month, order.getUserNum(), goods.getFeeFormula(), goods.getFeeBasenum());
			discount = mealPrivilege * orderMoney;
		}
		
		entity.setBuyType(1);
		entity.setCount(1);
		entity.setIsDredge(0);
		entity.setIsFree(0);
		entity.setGoodsId(order.getGoodsId());
		entity.setOccurTime(new Date());
		entity.setOrderMoney(orderMoney);
		entity.setDiscount(discount);
		entity.setOrderNum(orderNum);
		entity.setOverTime(overTime);
		entity.setParentId(order.getParentId());
		entity.setUseLife(userLife);
		entity.setUserId(order.getUserId());
		entity.setUserNum(order.getUserNum());
		entity.setPayByAccount(0.00);
		entity.setPayByOther(0.00);
		
		if(orderMoney == 0) {
			entity.setOrderState(1);
			entity.setPayMoney(0.00);
			entity.setPayTime(new Date());
			entity.setPayWayId(2);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//=============================新增一条交易记录开始======================================
			Deal deal = new Deal();
			deal.setDealDate(new Date());
			deal.setDealName(goods.getGoodsName());
			deal.setDealNum(orderNum);
			deal.setDealState("2");
			deal.setDisburseMoney(order.getOrderMoney());
			deal.setIncomeMoney(0.0);
			deal.setPayWayId(2);
			deal.setUserId(order.getUserId());
			deal.setParentId(user.getUserId());
			deal.setIsCancel(0);
			try {
				dealService.insertDeal(deal);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================新增一条交易记录结束======================================
			
			//=============================收支明细增加开始======================================
			//交易收支明细
			Cashflow cashflow = new Cashflow();
			cashflow.setCashflowNum(createNum.createCashflowNum("2"));
			cashflow.setCreateTime(new Date());
			cashflow.setDealType(2);
			cashflow.setDisburseMoney(0.00);
			cashflow.setIncomeMoney(0.00);
			cashflow.setOrderId(entity.getOrderId());
			cashflow.setPayWayId(2);
			cashflow.setThenAccount(user.getAccountMoney());
			cashflow.setUserId(user.getUserId());
			cashflow.setIsCancel(0);
			try {
				cashflowService.insertCashflow(cashflow);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================收支明细增加结束======================================
			
			//=============================普元数据同步开始======================================
			EOSresult = eos.sendProOrMeal(entity);
			if("ok".equals(EOSresult)) {
				//更改订单的开通状态
				Order updateOrder = orderService.selectOrderByOrderId(entity.getOrderId());
				updateOrder.setIsDredge(1);//0失败  1成功
				orderService.updateOrderByOrderId(updateOrder);
			}
			//=============================普元数据同步结束======================================
			mav = new ModelAndView("redirect:/buy/successOrder");
		}else {
			entity.setOrderState(0);
			entity.setPayMoney(orderMoney - discount);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mav = new ModelAndView("redirect:/myservice/uporder/submitOrders");
		}
		
		request.getSession().setAttribute("order", entity);
		request.getSession().setAttribute("userInfo", user);
		request.getSession().setAttribute("proMealName", goods.getGoodsName());
		request.getSession().setAttribute("EOSresult", EOSresult);
		request.getSession().setAttribute("BSTCOMPANYINFO", readProperties);
		return mav;
	}
	
	/**
	 * @return 提交订单并支付页面 
	 * @throws Exception
	 */
	@RequestMapping(value = "/uporder/submitOrders", method = RequestMethod.GET)
	public ModelAndView submitOrders(HttpServletRequest request) throws Exception {
		
		Order order = (Order) request.getSession().getAttribute("order");
		User user =  (User) request.getSession().getAttribute("userInfo");
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("order", order);
		mav.addObject("userInfo", user);
		mav.setViewName("users/myservice/uporders_pay");
		return mav;
	}
}
